﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InfoSys
{
    public partial class SysRoleWnd : UserControl
    {
        #region 成员函数
        private SysRole _currentQueryRecord;
        private PagerIndex _pagerIndex;
        private SysRole _selectedRecord;
        public SysRole SelectedRecord { get { return _selectedRecord; } }
        private CommonUI.WaitForm _waitForm = new CommonUI.WaitForm();
        #endregion

        #region 事件
        public delegate void ButtonClickHandler(object sender, EventArgs e);
        public event ButtonClickHandler BtnConfirmClick;
        public event ButtonClickHandler BtnCancelClick;

        #endregion

        #region 构造函数
        public SysRoleWnd()
        {
            InitializeComponent();
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 公共函数
        public void SetQueryState()
        {
            btnNew.Enabled = false;
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnConfirm.Visible = true;
            btnCancel.Visible = true;
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnNew.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnEdit.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnDelete.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.query;
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel2;
        }
        private void InitControl()
        {
            ResetDataPager();
            ResetQueryRecord();
            RefreshData();
        }

        private void ResetDataPager()
        {
            dataPager.PageCurrent = 1;
            dataPager.PageSize = CommonDefine.iPageSize;
        }

        private void ResetQueryRecord()
        {
            _currentQueryRecord = new SysRole();
        }

        private void RefreshData()
        {
            //读入部分数据
            _pagerIndex = new PagerIndex(dataPager.PageCurrent, CommonDefine.iPageSize);
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void Add()
        {
            SysRoleForm window = new SysRoleForm(EnumOpenType.New, null);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
            }
        }
        private void Edit()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要编辑的项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            SysRole record = row.DataBoundItem as SysRole;
            SysRoleForm window = new SysRoleForm(EnumOpenType.Open, record.Id);
            if (window.ShowDialog() == DialogResult.OK)
            {
                RefreshData();
            }
        }
        private void Delete()
        {
            if (dgv.CurrentCell == null)
                return;
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OK) == DialogResult.Cancel)
            {
                return;
            }

            DataGridViewRow row = dgv.Rows[rowIndex];
            SysRole record = row.DataBoundItem as SysRole;
            try
            {
                //删除操作
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                client.DeleteSysRole(record);

                //删除列表中的该行记录
                InitControl();
            }
            catch (System.Exception exc)
            {
                MessageBox.Show(this, "删除项出错，错误：" + exc.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MyDataBind(SysRole queryRecord, PagerIndex pIndex)
        {
            try
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                IList<SysRole> queryList = client.QuerySysRoleList(queryRecord, ref pIndex); 
                LoadBillDetail(queryList);
            }
            catch
            {
            }
        }

        private void LoadBillDetail(IList<SysRole> recordList)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                this.sysRoleBindingSource.DataSource = null;
                this.sysRoleBindingSource.DataSource = recordList;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
        #endregion

        #region 控件函数
        private void btnNew_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            ResetDataPager();
            ResetQueryRecord();
            _currentQueryRecord.Id = tbRoleCode.Text;
            _currentQueryRecord.RoleName = tbRoleName.Text;
            RefreshData();
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            MyDataBind(_currentQueryRecord, _pagerIndex);
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                MessageBox.Show("请首先在列表中选中项！");
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            _selectedRecord = row.DataBoundItem as SysRole;
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.OK;
                window.Close();
            }
            else
            {
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Parent is Form)
            {
                Form window = this.Parent as Form;
                window.DialogResult = DialogResult.Cancel;
                window.Close();
            }
            else
            {
                return;
            }
        }
        #endregion



    }
}
